<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\GoodsType;

class GoodsTypeController extends Controller
{
    //
    protected function index(){
    	
		$goodstype = GoodsType::select('id','name')->get();
		return view('admin.goodstype.index',array(
			'list'	=> $goodstype
			));
    }
	
	/*
	 * 添加商品类型
	 * 
	 * */
	protected function add(Request $request){
		
		if($request->isMethod('post')){
			$name = $request->input('name');
			$goodstype = new GoodsType;
			$goodstype->name = $name;
			if($goodstype->save()){
				return response()->json([
					'code'	=> '00000',
					'msg'	=> '保存成功',
					'url'	=> route('admin.goodstype.index')
					]);
			}else{
				return response()->json([
					'code'	=> '10001',
					'msg'	=> '保存失败！',
					'url'	=> ''
					]);				
			}
		}

		return view('admin.goodstype.add');
	}
	
	/*
	 * 删除
	 * */
	protected function del($id){
		$goodstype = GoodsType::find($id);
		if($goodstype->delete()){
			return response()->json([
				'code'	=> '00001',
				'msg'	=> '删除成功！',
				'url'	=> ''
				]);			
		}else{
			return response()->json([
				'code'	=> '10001',
				'msg'	=> '删除失败！',
				'url'	=> ''
				]);
		}
	}
	
	/*
	 * 编辑
	 * */
	protected function edit(Request $request,$id){
		if($request->isMethod('post')){
			$name = $request->input('name');
			$goodstype = GoodsType::find($id);
			$goodstype->name = $name;
			if($goodstype->save()){
				return response()->json([
					'code'	=> '00000',
					'msg'	=> '保存成功！',
					'url'	=> route('admin.goodstype.index')
					]);			
			}else{
				return response()->json([
					'code'	=> '10001',
					'msg'	=> '保存失败！',
					'url'	=> ''
					]);
			}
			
		}
		
		$goodstype = GoodsType::select('id','name')->find($id);
		return view('admin.goodstype.edit',[
			'goodstype'	=> $goodstype
			]);
		
	}
}
